package com.lzs.chuku.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzs.chuku.mapper.OutorderDetailMapper;
import com.lzs.chuku.mapper.UserMapper;
import com.lzs.chuku.service.ReviewDetailService;
import com.lzs.entity.OutorderDetail;
import com.lzs.entity.Review;
import com.lzs.chuku.service.ReviewService;
import com.lzs.entity.ReviewDetail;
import com.lzs.entity.User;
import com.lzs.util.JwtUtils;
import com.lzs.util.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 刘中帅
 * @since 2020-11-23
 */
@RestController
@RequestMapping("/chuku/review")
public class ReviewController {
    @Autowired
    private ReviewDetailService reviewDetailService;
    @Autowired
    private ReviewService reviewService;
    @Autowired
    private OutorderDetailMapper outorderDetailMapper;
    @Autowired
    private UserMapper userMapper;
    @PostMapping("addReview")
    @ApiOperation("新增出库复核")
    public R addReview(@RequestBody Review review, HttpServletRequest request){
        String token = request.getHeader("token");
        Long userId= JwtUtils.getMemberIdByJwtToken(token);
        User user = userMapper.findUserbyId(userId);
        review.setCreateid(user.getUserId());
        review.setCreator(user.getName());
        return reviewService.addReview(review);
    }

    @PostMapping("findByPageReview/{currentPage}/{pageSize}")
    @ApiOperation("分页查询出库复核")
    public R findByPageReview(@RequestBody Review review,
                              @PathVariable("currentPage") Integer currentPage,
                              @PathVariable("pageSize") Integer pageSize){
        return reviewService.findByPageReview(review,currentPage,pageSize);
    }
    @GetMapping("overReview/{rid}")
    @ApiOperation("结束复核")
    public R overReview(@PathVariable("rid") Long reviewid){
        return reviewService.orveReview(reviewid);
    }

    @PostMapping("reviewing")
    @ApiOperation("录入明细开始改状态为复核中")
    public R reviewing(@RequestBody Review review){
        return reviewService.changeState(review.getId(), "2");
    }

    @PostMapping("deleteReview")
    @ApiOperation("删除出库复核")
    public R deleteReview(@RequestBody Review review){
        QueryWrapper<ReviewDetail> wrapper=new QueryWrapper<>();
        wrapper.eq("pid",review.getId());
        reviewDetailService.remove(wrapper);
        boolean b = reviewService.removeById(review.getId());
        if (b){
            return R.ok().message("删除成功");
        }else{
            return R.error().message("删除失败");
        }
    }
    
    @GetMapping("findAllcommByReviewId/{id}")
    @ApiOperation("根据复核id查找所有对应出库订单中的商品")
    public R findAllcommByReviewId(@PathVariable("id") Long id){
        try {
            Review review = reviewService.getById(id);
            QueryWrapper<OutorderDetail> wrapper = new QueryWrapper<>();
            wrapper.eq("pid", review.getOutorderid());
            List<OutorderDetail> outorderDetails = outorderDetailMapper.selectList(wrapper);
            return R.ok().data("list",outorderDetails);
        }catch (RuntimeException e){
            return R.error();
        }
    }
}

